﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


using YT.DB;
using YT.Commons;

namespace YT.XH.MeniereDisease
{
    public partial class frmMDTherapeuticMethodMtn : Form
    {

        private string mstrSickRecID = string.Empty;
        private DBAccess mdbcon = new DBAccess(false, false);
        private frmMDTheraputicMethodDetail frmThera1 = null;
        private frmMDTheraputicMethodDetail frmThera2 = null;
        private frmMDTheraputicMethodDetail frmThera3 = null;
        
        private void changetext ( string strPeriod )
        {
            if (strPeriod == "1")
            {
                if (!tcbMain.TabPages.Contains(tabPage2))
                {
                    AddTheraToPanel(2);
                    tcbMain.TabPages.Add(tabPage2);
                    pnlCaseAndSel1.Enabled = false;
                }
                tcbMain.SelectedTab = tabPage2;
            }
            else if (strPeriod == "2")
            {
                if (!tcbMain.TabPages.Contains(tabPage3))
                {
                    AddTheraToPanel(3);
                    tcbMain.TabPages.Add(tabPage3);
                    pnlCaseAndSel1.Enabled = false;
                    pnlCaseAndSel2.Enabled = false;
                }
                tcbMain.SelectedTab = tabPage3;
            }
            
        }        

        public frmMDTherapeuticMethodMtn()
        {
            InitializeComponent();
            mstrSickRecID = SysVar.SickRecID;
            
                
        }
        //private string GetOperationNameFromRad()
        //{
        //    if (rad方案1.Checked) return "方案1";
        //    if (rad方案2.Checked) return "方案2";
        //    if (rad方案3.Checked) return "方案3";
        //    if (rad方案4.Checked) return "方案4";
        //    if (rad方案5.Checked) return "方案5";
            
        //    return "";
        //}

        //private void GetRadFromOperationName(int intPeriod, string strOperationName)
        //{
        //    if (intPeriod == 1)
        //    {
        //        if (strOperationName == "方案1") rad方案1.Checked = true;
        //        if (strOperationName == "方案2") rad方案2.Checked = true;
        //        if (strOperationName == "方案3") rad方案3.Checked = true;
        //        if (strOperationName == "方案4") rad方案4.Checked = true;
        //        if (strOperationName == "方案5") rad方案5.Checked = true;
        //    }
        //    else if (intPeriod == 2)
        //    {
        //        if (strOperationName == "方案6") rad方案1.Checked = true;
        //        if (strOperationName == "方案7") rad方案2.Checked = true;
        //        if (strOperationName == "方案8") rad方案3.Checked = true;
        //        if (strOperationName == "方案9") rad方案4.Checked = true;
        //        if (strOperationName == "方案10") rad方案5.Checked = true;
        //    }
        //    else if (intPeriod == 3)
        //    {
        //        if (strOperationName == "方案11") rad方案1.Checked = true;
        //        if (strOperationName == "方案12") rad方案2.Checked = true;
        //        if (strOperationName == "方案13") rad方案3.Checked = true;
        //        if (strOperationName == "方案14") rad方案4.Checked = true;
        //        if (strOperationName == "方案15") rad方案5.Checked = true;
        //    }
        //}
        public frmMDTherapeuticMethodMtn(string strSickRecID)
        {
            InitializeComponent();
            mstrSickRecID = strSickRecID; 
        }

        private void btnContraindication_Click(object sender, EventArgs e)
        {
            frmMDMethodMtn frmMethodMtn = new frmMDMethodMtn();
            frmMethodMtn.Width =790;
            frmMethodMtn.Height =510;
            frmMethodMtn.StartPosition = FormStartPosition.CenterScreen;
            //frmMethodMtn.MdiParent = SysVar.mfrmMdiPar;
            frmMethodMtn.ShowDialog();
        }

        private void frmMDTherapeuticMethodMtn_Load(object sender, EventArgs e)
        {
            this.Text = "治疗方案";
            FillCurOperation();
            timer1.Start();

            frmMDInculde frmInculde = new frmMDInculde();
            if (!frmInculde.ShowForm())
            {
                this.Close();
                return;
            }

            string strCurHospitalID = DataFunctions.GetFieldValue("mSickPerson", "HospitalID", "SickRecID='" + mstrSickRecID + "'");
            bool blnHasMeniett = CvtFunc.ToBoolean(DataFunctions.GetFieldValue("mHospital", "HasMeniett", "HospitalID='" + strCurHospitalID + "'"));
            if (blnHasMeniett)
                有Meniett仪器ToolStripMenuItem.Checked = true;
            else
                有Meniett仪器ToolStripMenuItem.Checked = false;

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            UpdateData();
            this.Close();                        
        }

        private bool UpdateData()
        {
            try
            {
                this.frmThera1.UpdateTheraputicMethodDetail();
                if ( frmThera2 !=null )  frmThera2.UpdateTheraputicMethodDetail();
                if ( frmThera3 !=null) frmThera3.UpdateTheraputicMethodDetail();
                string strSQL = string.Empty;
                string strCurOperation = string.Empty;
                strSQL = "select TreatCaseName from mTreatHistory where Period in (select max(Period) from mTreatHistory where SickRecID='" + mstrSickRecID + "' and TreatCaseName<>'') and SickRecID='" + mstrSickRecID + "' order by recid desc";
                DataRow drCurCase = mdbcon.GetDataFirstRow(strSQL);
                if (drCurCase != null)
                {
                    strCurOperation = CvtFunc.ToString(drCurCase["TreatCaseName"]);
                }
                strSQL = "update mSickPerson set CurOperation='" + strCurOperation + "'  where sickrecid='" + mstrSickRecID + "'  ";
                mdbcon.ExecuteSql(strSQL);

               
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return false;
            }
            return true;
        }

        private void FillCurOperation()
        {

            //治疗前检查的“口服甘油前” 0.5K, 1K,2K,3K 的气导算术平均值。
            string strSQL = " SELECT SUM(CAST(Result AS int)) FROM ExamBeforTreatResult WHERE (HdrSeqNo = '22') " +
                          " AND (DtlSeqNo IN ('2', '3', '4', '5', '9', '10', '11', '12')) AND " +
                          " (RowNo = 'B') and SickRecID='"+ mstrSickRecID  +"' and Type='"+ TitleType.MeniereDisease  +"' and isnumeric(Result) = 1 ";
            double dblAmt = mdbcon.GetDoubleValue(strSQL);
            txtPTA.Text ="此患者的PTA为： " +  BaseFunctions.FormatNumeric(  CvtFunc.ToString  ( dblAmt /8.00 ),"N.0") ;


           
            strSQL = "select * from mSickPerson where sickrecid='" + mstrSickRecID + "'";
            DataRow drRow = mdbcon.GetDataFirstRow(strSQL);
            if (drRow == null) return;
                                            
            strSQL ="select * from mTreatHistory where SickRecID='" + mstrSickRecID  + "'  " +
                    " and recid in "+
                    " ( "+
                    " select max(recid) as recid from mTreatHistory where sickrecid='" + mstrSickRecID + "' group by sickrecid,period " +
                    " ) "+
                    " order by Period";
            DataTable dtCase = mdbcon.GetDataTable(strSQL);
            if ( dtCase.Rows.Count ==0)
            {
                AddTheraToPanel(1);
                AddTheraToPanel(2);
                AddTheraToPanel(3);
                tcbMain.TabPages.Remove(tabPage2);
                tcbMain.TabPages.Remove(tabPage3);
            }
            else 
            {
                int intMaxPeriod = 0;

                //DataRow drCaseRow = dtCase.Rows[dtCase.Rows.Count - 1];
                foreach (DataRow drCaseRow in dtCase.Rows)
                {
                    int intPeriod = CvtFunc.ToInt(drCaseRow["Period"]);
                    intMaxPeriod = intPeriod;
                    string strOperationName = CvtFunc.ToString(drCaseRow["TreatCaseName"]);
                    AddTheraToPanel(intPeriod);
                }
                if (intMaxPeriod == 3)
                {
                    groupBox1.Enabled = false;
                    pnlCaseAndSel1.Enabled = false; 
                    groupBox2.Enabled = false;
                    pnlCaseAndSel2.Enabled = false;
                }
                if (intMaxPeriod == 2)
                {
                    groupBox1.Enabled = false;
                    pnlCaseAndSel1.Enabled = false; 
                }

                if (intMaxPeriod == 1)
                {
                    tcbMain.TabPages.Remove(tabPage2);
                    tcbMain.TabPages.Remove(tabPage3);
                }
                if (intMaxPeriod == 2)
                {
                    tcbMain.TabPages.Remove(tabPage3);
                }

            }
        }
               

        private void btnExit_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否保存？", "", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
            {
                if (UpdateData()) this.Close();
            }
            else
                this.Close();
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void frmMDTherapeuticMethodMtn_FormClosed(object sender, FormClosedEventArgs e)
        {
            SysVar.marrFormList.Remove(this.Name);
        }

        private void AddTheraToPanel( int intPeriod )
        {
            if (intPeriod == 1)
            {
                if (frmThera1 != null)
                {
                    frmThera1 = null;                    
                }
                frmThera1 = new frmMDTheraputicMethodDetail(intPeriod, mstrSickRecID);
                FillTheatForm(frmThera1, pnlCaseAndSel1);
                frmThera1.onchange += new frmMDTheraputicMethodDetail.change(changetext);
            }
            else if (intPeriod == 2)
            {
                if (frmThera2 != null)
                {
                    frmThera2 = null;                    
                }
                frmThera2 = new frmMDTheraputicMethodDetail(intPeriod, mstrSickRecID);
                FillTheatForm(frmThera2, pnlCaseAndSel2);
                frmThera2.onchange += new frmMDTheraputicMethodDetail.change(changetext);
            }
            else if (intPeriod == 3)
            {
                if (frmThera3 != null)
                {
                    frmThera3 = null;                    
                }
                frmThera3 = new frmMDTheraputicMethodDetail(intPeriod, mstrSickRecID);
                FillTheatForm(frmThera3, pnlCaseAndSel3);
                frmThera3.onchange += new frmMDTheraputicMethodDetail.change(changetext);
            }
        }

        private void FillTheatForm(Form frmThera, Panel pnlCaseAndSel)
        {
            frmThera.TopMost = false;
            frmThera.TopLevel = false;
            frmThera.Dock = DockStyle.Fill;
            frmThera.FormBorderStyle = FormBorderStyle.None;
            pnlCaseAndSel.Controls.Add(frmThera);
            frmThera.Location = new Point(0, 0);
            frmThera.Dock = DockStyle.Fill;
            frmThera.Size = new Size(pnlCaseAndSel.Width, pnlCaseAndSel.Height);
            frmThera.Visible = true;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (this.Opacity >= 1)
                timer1.Stop();
            this.Opacity += 0.04;
        }

        private void btnInclude_Click(object sender, EventArgs e)
        {
            frmMDInculde frmInculde = new frmMDInculde();
            frmInculde.ShowDialog(); 
        }

        private void 有Meniett仪器ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            bool blnMeniett = 有Meniett仪器ToolStripMenuItem.Checked;
            string strCurHospitalID = DataFunctions.GetFieldValue("mSickPerson", "HospitalID", "SickRecID='" + mstrSickRecID + "'");
            string strSQL = "update mHospital set HasMeniett = " + (blnMeniett ? 1 : 0) + "  where HospitalID ='" + strCurHospitalID + "'";
            mdbcon.ExecuteSql(strSQL);

            frmThera1.Close();
            frmThera2.Close();
            frmThera3.Close();
            FillCurOperation();
        }
    }
}
